﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Venues
{
    public partial class AnnexeList : System.Web.UI.Page
    {
        private Jy123.Model.User_Main user;
        private bool isVenues = false;
        private bool isTCenter = false;
        private Jy123.Model.Module_Main module;
        protected void Page_Load(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            string type = Request.QueryString["Type"];
            string pType = Request.QueryString["PType"];
            string action = Request["Action"];
            string strVId = Request.QueryString["VenuesId"];
            if (string.IsNullOrEmpty(strVId))
            {
                strVId = Request["VenuesId"];
            }
            if (!string.IsNullOrEmpty(strVId))
            {
                int moduleId = int.Parse(strVId);
                Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                module = BLLModule.GetModel(moduleId);
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
                int jFather = pJType.JFather.Value;
                if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main) || jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.TCenter_Main))
                {
                    isVenues = true;
                }
                if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.TCenter_Main))
                {
                    isTCenter = true;
                }
                user = BLLUser.GetModel(module.FUser.Value);

                if (!string.IsNullOrEmpty(type))
                {
                    string html = string.Empty;
                    switch (type)
                    {
                        case "Annexe":
                            Jy123.BLL.Annexe_Record BLLAR = new Jy123.BLL.Annexe_Record();
                            string strWhere = string.Empty;
                            if (!string.IsNullOrEmpty(pType))
                            {
                                strWhere = string.Format("FModule = {0} AND FAnnexe IN (SELECT ID FROM Public_Annexe WHERE FAType = {1})", strVId, pType);
                            }
                            else
                            {
                                if (isTCenter)
                                {
                                    strWhere = string.Format("FModule = {0} AND FAnnexe IN (SELECT ID FROM Public_Annexe WHERE FAType = 9)", strVId);
                                }
                                else
                                {
                                    strWhere = string.Format("FModule = {0} AND FAnnexe IN (SELECT ID FROM Public_Annexe WHERE FAType <> 6 AND FAType <> 8)", strVId);
                                }
                            }

                            List<Jy123.Model.Annexe_Record> listRecord = BLLAR.GetModelList(strWhere);
                            html = BindDetail(listRecord);
                            break;
                        case "Props":
                            Jy123.BLL.Club_PropsRecord BLLProps = new Jy123.BLL.Club_PropsRecord();
                            string strClubWhere = string.Format("FUser = {0}", user.ID.ToString());
                            if (!string.IsNullOrEmpty(pType))
                            {
                                strClubWhere = string.Format("FUser = {0} AND FProps IN (SELECT ID FROM Public_Props WHERE ((FPType IN (SELECT ID FROM Public_PropsType WHERE PFather = {1})) AND FJType = {2}) OR FPType = {1})", user.ID.ToString(), pType, module.FJType.Value.ToString());
                            }

                            if (isVenues)
                            {
                                //赛场只拥有加油捣蛋道具
                                strClubWhere = string.Format("FUser = {0} AND FProps IN (SELECT ID FROM Public_Props WHERE FPType = {1})", user.ID.ToString(), pType);
                            }
                            List<Jy123.Model.Club_PropsRecord> listPropsRecord = BLLProps.GetModelList(strClubWhere);
                            html = BindDetail(listPropsRecord);
                            break;
                        case "AnnexeStore":
                            Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
                            string strAWhere = "FJType = " + module.FJType.Value.ToString() + " AND FAType <> 6 AND FAType <> 8";//广告内容不算设施
                            if (isTCenter)
                            {
                                strAWhere = "FJType = " + module.FJType.Value.ToString() + " AND FAType = 9";
                            }
                            if (!string.IsNullOrEmpty(pType))
                            {
                                strAWhere = "FJType = " + module.FJType.Value.ToString() + " AND FAType = " + pType;
                            }
                            List<Jy123.Model.Public_Annexe> listAnnexe = BLLAnnexe.GetModelList(strAWhere);
                            html = BindDetail(listAnnexe);
                            break;
                        case "PropsStore":
                            Jy123.BLL.Public_Props BLLPProps = new Jy123.BLL.Public_Props();
                            string strPWhere = string.Format("FJType = {0} OR FPType = 6 OR FPType = 21", module.FJType.Value.ToString(), pType);
                            if (!string.IsNullOrEmpty(pType))
                            {
                                if (!pType.Equals("6"))
                                {
                                    strPWhere = string.Format("(FJType = {0} AND (FPType = {1} OR FPType IN(SELECT ID FROM Public_PropsType WHERE PFather = {1})))", module.FJType.Value.ToString(), pType);
                                    if (pType.Equals("21"))
                                    {
                                        strPWhere = "FPType = 21";
                                    }
                                }
                                else
                                {
                                    strPWhere = "FPType = 6";
                                }

                            }
                            if (isVenues)
                            {
                                //赛场只能购买加油捣蛋道具
                                strPWhere = "FPType = 6";
                            }
                            List<Jy123.Model.Public_Props> listProps = BLLPProps.GetModelList(strPWhere);
                            html = BindDetail(listProps);
                            break;
                    }
                    if (string.IsNullOrEmpty(html))
                    {
                        html = "暂无此类商品";
                    }
                    Response.Write(html);
                }
                else if (!string.IsNullOrEmpty(action))
                {
                    string strId = Request["Id"];
                    if (!string.IsNullOrEmpty(strId))
                    {
                        int id = int.Parse(strId);
                        switch (action.ToLower())
                        {
                            case "props":
                                BLL.Props.Props BLLEXProps = new Jy123.BLL.Props.Props();
                                BLLEXProps.BuyProps(user, id, 1, module.ID, jFather);//需要改进
                                break;
                            case "annexe":
                                BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
                                Model.Public_Annexe annexe = BLLAnnexe.GetModel(id);
                                BLL.Annexe.Annexe BLLEXAnnexe = new Jy123.BLL.Annexe.Annexe();
                                BLLEXAnnexe.BuyAnnexe(annexe, module.ID, user);
                                break;
                        }

                    }
                }
                string strValue = "0";
                if (jFather == Convert.ToInt32(BLL.Public.EnumConfig.JyType.Venues_Main))
                {
                    BLL.Venues_Main BLLVenues = new BLL.Venues_Main();
                    Model.Venues_Main venues = BLLVenues.GetModel(moduleId);
                    if (venues.Assets.HasValue)
                    {
                        strValue = (Convert.ToInt32(venues.Assets.Value)).ToString();
                    }
                }
                else if (jFather == Convert.ToInt32(BLL.Public.EnumConfig.JyType.Club_Main))
                {
                    BLL.Club_Main BLLClub = new BLL.Club_Main();
                    Model.Club_Main club = BLLClub.GetModel(moduleId);
                    if (club.Assets.HasValue)
                    {
                        strValue = (Convert.ToInt32(club.Assets.Value)).ToString();
                    }
                }
                else
                {

                }
                string strAssets = string.Format("总资产：<span class=\"FontRedB\">{0}</span>", strValue);
                user = BLLUser.GetModel(module.FUser.Value);
                string strCash = string.Format("现金：<span class=\"FontRedB\">{0}</span>", user.Cash.Value.ToString());
                StringBuilder sbString = new StringBuilder();
                sbString.Append("<script type='text/javascript' language='javascript'>");
                sbString.AppendFormat("$(\"#assetsLi\").html('{0}');", strAssets);
                sbString.AppendFormat("$(\"#cashLi\").html('{0}');", strCash);
                //sbString.Append("$(\"#cashLi\").html('0');");
                sbString.Append("</script>");
                Response.Write(sbString.ToString());
            }
        }

        private string BindDetail(List<Jy123.Model.Annexe_Record> listRecord)
        {
            StringBuilder strResult = new StringBuilder();
            Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
            List<Jy123.Model.Public_Annexe> listAnnexe = BLLAnnexe.GetModelList(string.Empty);
            Dictionary<int, Jy123.Model.Public_Annexe> dicAnnexe = new Dictionary<int, Jy123.Model.Public_Annexe>();
            foreach (Jy123.Model.Public_Annexe annexe in listAnnexe)
            {
                dicAnnexe.Add(annexe.ID, annexe);
            }
            foreach (Jy123.Model.Annexe_Record record in listRecord)
            {
                string strDetail = Cache["annexedetail"].ToString();
                Jy123.Model.Public_Annexe cAnnexe = dicAnnexe[record.FAnnexe.Value];
                strDetail = strDetail.Replace("$DetailImageSrc$", cAnnexe.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cAnnexe.AName);
                strDetail = strDetail.Replace("$DetailEffects$", "收入");
                if (cAnnexe.Earnings.HasValue)
                {
                    strDetail = strDetail.Replace("$DetailValue$", "+" + Convert.ToInt32(cAnnexe.Earnings.Value).ToString());
                }
                else
                {
                    strDetail = strDetail.Replace("$DetailValue$", "无");
                }
                string strDes = cAnnexe.Description;
                if (strDes.Length > 7)
                {
                    strDes = strDes.Substring(0, 7) + "...";
                }
                strDetail = strDetail.Replace("$DetailUse$", strDes);
                strDetail = strDetail.Replace("$Style$", "style='display:none'");
                strDetail = strDetail.Replace("$ClickEvent$", string.Empty);
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private string BindDetail(List<Jy123.Model.Club_PropsRecord> listPropsRecord)
        {
            StringBuilder strResult = new StringBuilder();
            Jy123.BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            List<Jy123.Model.Public_Props> listProps = BLLProps.GetModelList(string.Empty);
            Dictionary<int, Jy123.Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();
            foreach (Jy123.Model.Public_Props props in listProps)
            {
                dicProps.Add(props.ID, props);
            }
            foreach (Jy123.Model.Club_PropsRecord record in listPropsRecord)
            {
                string strDetail = Cache["annexedetail"].ToString();
                Jy123.Model.Public_Props cProps = dicProps[record.FProps.Value];
                strDetail = strDetail.Replace("$DetailImageSrc$", cProps.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cProps.PName);
                string strEffext = string.Empty;
                string strValue = string.Empty;
                if (cProps.Liveness.HasValue)
                {
                    strEffext = "活力";
                    if (cProps.Liveness.Value > 0)
                    {
                        strValue = "+" + cProps.Liveness.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Liveness.Value.ToString();
                    }
                }
                else if (cProps.Moral.HasValue)
                {
                    strEffext = "士气";
                    if (cProps.Moral.Value > 0)
                    {
                        strValue = "+" + cProps.Moral.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Moral.Value.ToString();
                    }
                }
                else if (cProps.Skill.HasValue)
                {
                    strEffext = "技术";
                    if (cProps.Skill.Value > 0)
                    {
                        strValue = "+" + cProps.Skill.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Skill.Value.ToString();
                    }
                }
                else if (cProps.PhysicalFitness.HasValue)
                {
                    strEffext = "体力";
                    if (cProps.PhysicalFitness.Value > 0)
                    {
                        strValue = "+" + cProps.PhysicalFitness.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.PhysicalFitness.Value.ToString();
                    }
                }
                strDetail = strDetail.Replace("$DetailEffects$", strEffext);
                strDetail = strDetail.Replace("$DetailValue$", strValue);
                string strDes = cProps.Description;
                if (strDes.Length > 7)
                {
                    strDes = strDes.Substring(0, 7) + "...";
                }
                strDetail = strDetail.Replace("$DetailUse$", strDes);
                strDetail = strDetail.Replace("$Style$", "style='display:none'");
                strDetail = strDetail.Replace("$ClickEvent$", string.Empty);
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private string BindDetail(List<Jy123.Model.Public_Annexe> listAnnexe)
        {
            StringBuilder strResult = new StringBuilder();
            foreach (Jy123.Model.Public_Annexe cAnnexe in listAnnexe)
            {
                string strDetail = Cache["annexedetail"].ToString();
                strDetail = strDetail.Replace("$DetailImageSrc$", cAnnexe.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cAnnexe.AName);
                strDetail = strDetail.Replace("$DetailEffects$", "收入");

                if (cAnnexe.Earnings.HasValue)
                {
                    strDetail = strDetail.Replace("$DetailValue$", "+" + Convert.ToInt32(cAnnexe.Earnings.Value).ToString());
                }
                else
                {
                    strDetail = strDetail.Replace("$DetailValue$", "无");
                }

                string strDes = cAnnexe.Description;
                if (strDes.Length > 7)
                {
                    strDes = strDes.Substring(0, 7) + "...";
                }
                strDetail = strDetail.Replace("$DetailUse$", strDes);
                strDetail = strDetail.Replace("$Style$", "style='display:block'");
                string strEvent = string.Empty;
                string usingLevel = cAnnexe.UsingLevel;
                BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                Model.Venues_Main venues = BLLVenues.GetModel(module.ID);
                BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                Model.TCenter_Main TCenter = BLLTCenter.GetModel(module.ID);
                BLL.Annexe_Record BLLARecord = new Jy123.BLL.Annexe_Record();
                List<Model.Annexe_Record> listRecord = BLLARecord.GetModelList("FModule = " + module.ID.ToString());
                bool canUse = false;

                if (isTCenter)
                {
                    canUse = string.IsNullOrEmpty(usingLevel) || usingLevel.Contains(TCenter.FLevel.Value.ToString());
                }
                else
                {
                    canUse = string.IsNullOrEmpty(usingLevel) || usingLevel.Contains(venues.FLevel.Value.ToString());
                }

                string strImageSrc = "/Images/Module/Buy.gif";
                if (user.Cash < cAnnexe.Price.Value)
                {
                    strEvent = "$.moonbox.warning('您的现金不足！')";
                }
                else if (IsInList(cAnnexe, listRecord))
                {
                    strEvent = "$.moonbox.warning('您已经拥有这件商品了！')";
                    strImageSrc = "/Images/Module/AlreadyBuy.gif";
                }
                else if (!canUse)
                {
                    strEvent = "$.moonbox.warning('该商品不适合您的组件使用！')";
                    strImageSrc = "/Images/Module/CanNotUse.gif";
                }
                else
                {
                    strEvent = string.Format("PostAjax('/Module/Venues/AnnexeList.aspx','Action=Annexe&VenuesId={0}&Id={1}');fillhtml('listUl','/Module/Venues/AnnexeList.aspx?Type=AnnexeStore&VenuesId='+$('#venuesHidden').val()+'&PType='+$('#selTypeHidden').val()+'&'+ Math.random())", module.ID.ToString(), cAnnexe.ID.ToString());
                }
                strDetail = strDetail.Replace("$ImageSrc$", strImageSrc);
                strDetail = strDetail.Replace("$ClickEvent$", strEvent);
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private string BindDetail(List<Jy123.Model.Public_Props> listProps)
        {
            StringBuilder strResult = new StringBuilder();
            foreach (Jy123.Model.Public_Props cProps in listProps)
            {
                string strDetail = Cache["annexedetail"].ToString();
                strDetail = strDetail.Replace("$DetailImageSrc$", cProps.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cProps.PName);
                string strEffext = string.Empty;
                string strValue = string.Empty;
                if (cProps.Liveness.HasValue)
                {
                    strEffext = "活力";
                    if (cProps.Liveness.Value > 0)
                    {
                        strValue = "+" + cProps.Liveness.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Liveness.Value.ToString();
                    }
                }
                else if (cProps.Moral.HasValue)
                {
                    strEffext = "士气";
                    if (cProps.Moral.Value > 0)
                    {
                        strValue = "+" + cProps.Moral.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Moral.Value.ToString();
                    }
                }
                else if (cProps.Skill.HasValue)
                {
                    strEffext = "技术";
                    if (cProps.Skill.Value > 0)
                    {
                        strValue = "+" + cProps.Skill.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.Skill.Value.ToString();
                    }
                }
                else if (cProps.PhysicalFitness.HasValue)
                {
                    strEffext = "体力";
                    if (cProps.PhysicalFitness.Value > 0)
                    {
                        strValue = "+" + cProps.PhysicalFitness.Value.ToString();
                    }
                    else
                    {
                        strValue = cProps.PhysicalFitness.Value.ToString();
                    }
                }
                strDetail = strDetail.Replace("$DetailEffects$", strEffext);
                strDetail = strDetail.Replace("$DetailValue$", strValue);
                string strDes = cProps.Description;
                if (strDes.Length > 7)
                {
                    strDes = strDes.Substring(0, 7) + "...";
                }
                strDetail = strDetail.Replace("$DetailUse$", strDes);
                strDetail = strDetail.Replace("$Style$", "style='display:block'");
                string strEvent = string.Empty;
                string usingLevel = cProps.UsingLevel;
                int fLevel = 0;
                BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Model.Public_JType JType = BLLJType.GetModel(module.FJType.Value);
                if (JType.JFather.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main))
                {
                    BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                    Model.Venues_Main venues = BLLVenues.GetModel(module.ID);
                    fLevel = venues.FLevel.Value;
                }
                else if (JType.JFather.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Club_Main))
                {
                    BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                    Model.Club_Main club = BLLClub.GetModel(module.ID);
                    fLevel = club.FLevel.Value;
                }

                bool canUse = string.IsNullOrEmpty(usingLevel) || usingLevel.Contains(fLevel.ToString()) || cProps.FPType.Value == 6 || cProps.FPType.Value == 21;
                string strImageSrc = "/Images/Module/Buy.gif";
                if (user.Cash < cProps.Price.Value)
                {
                    strEvent = "$.moonbox.warning('您的现金不足！')";
                }
                else if (!canUse)
                {
                    strEvent = "$.moonbox.warning('该商品不适合您的组件使用！')";
                    strImageSrc = "/Images/Module/CanNotUse.gif";
                }
                else
                {
                    strEvent = string.Format("PostAjax('/Module/Venues/AnnexeList.aspx','Action=Props&VenuesId={0}&Id={1}');fillhtml('listUl','/Module/Venues/AnnexeList.aspx?Type=PropsStore&VenuesId='+$('#venuesHidden').val()+'&PType='+$('#selTypeHidden').val()+'&'+ Math.random())", module.ID.ToString(), cProps.ID.ToString());
                }
                strDetail = strDetail.Replace("$ImageSrc$", strImageSrc);
                strDetail = strDetail.Replace("$ClickEvent$", strEvent);
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private bool IsInList(Model.Public_Annexe annexe, List<Model.Annexe_Record> listRecord)
        {
            foreach (Model.Annexe_Record record in listRecord)
            {
                if (annexe.ID == record.FAnnexe)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
